package com.open.capacity.prints.controller;


import com.open.capacity.commons.Result;
import com.open.capacity.prints.entity.SysPrintTree;
import com.open.capacity.prints.service.SysPrintTreeService;
import com.open.capacity.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.list.TreeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "打印模板列表api")
public class SysPrintTreeController {


    @Autowired
    private SysPrintTreeService PrintTreeService;




    @PreAuthorize("hasAuthority('printTree:get/printTrees')")
    @ApiOperation(value = "查询区域tree")
    @GetMapping("/printTrees/tree")
    public Result<List<TreeVo>> printTrees(@RequestParam(value = "parentId", defaultValue = "0") String parentId) {
        List<SysPrintTree> printTrees = PrintTreeService.findByParentId(parentId);
        List<TreeVo> treeList = new TreeList();
        printTrees.forEach(m -> {
            TreeVo node = new TreeVo();
            node.setId(m.getId());
            node.setText(m.getAreaName());
            node.setValue(m.getId());
            node.setParentId(m.getParentId());
            List<SysPrintTree> childs = PrintTreeService.findByParentId(node.getId());
            node.setHasChildren(!CollectionUtils.isEmpty(childs));

            treeList.add(node);
        });

        return Result.succeed(treeList);
    }

}

